<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta name="keywords" content=""/>
    <meta name="description" content=""/>
    <title>设置分管小区</title>
    <link rel="stylesheet" href="../layui/css/layui.css"/>
    <link rel="stylesheet" href="../css/admin.css"/>
    <link rel="stylesheet" href="../fonts/iconfont.css">
    <style>
        body {
            height: 200px;
        }
        #demo {
            margin: 30px 76px;
        }
        .layui-tree li .layui-tree-check {
            color: #000;
            cursor: pointer;
            font-weight: 700;
        }
        .layui-tree li a i {
        }
        #btn-c, #btn-s {
            border: 0;
            margin-top: 3px;
            width: 90px;
            height: 24px;
            border-radius: 5px;
            background-color: #D8D8D8;
        }
        #btn-c {
            margin-left: 70px;
        }
        #btn-s:hover, #btn-c:hover {
            color: #fff;
            background-color: #EF7632;
        }
        #btn-s {
            margin-left: 15px;
        }
        .layui-layer .layui-layer-title {
            background-color: #fff;
        }

        .layui-form-checkbox[lay-skin=primary] i {
            position: absolute;
        }

        .layui-form-checkbox[lay-skin=primary] span {
            float: none;
            margin-left: 15px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <div id="xtree" class="xtree_contianer" style="width:280px;border:none;padding: 10px 0 25px 10px;"></div>
    <input type="button" id="btn-c" value="取消">
    <input type="button" id="btn-s" value="保存">
</form>
<script type="text/javascript" src="../layui/layui.js"></script>
<script type="text/javascript" src="../layui-xtree/layui-xtree.js"></script>
<script>
    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery;
        form = layui.form();
        layer = layui.layer;

       /* var wuyeID = localStorage.getItem("wuyeID");*/
        var set_Up = localStorage.getItem("set_Up");
        var userId = localStorage.getItem("user_id");
        var params = {};

        $.ajax({
            url: '../../wuye_account/getWholeRegion',
            type: 'GET',
            dataType: "json",
            data: {
                wuye_id:set_Up
            }
        })
            .done(function (response) {
                console.log(response);
                if (response.resultCode == 200) {
                    console.log("Get all success");
                    var data = response.data;
                    console.log(data);

                    var buildingResult = getArray(data, [], 'building');
                    var districtResult = getArray(data, [], 'district');

                    params.buildingResult = buildingResult;
                    console.log(params.buildingResult);

                    params.districtResult = districtResult;
                    console.log(params.districtResult);

                    //查找末节点
                    function getArray(arrData, totalArr, name) {

                        arrData.map(value => {
                            if (value.data.length === 0) {
                                if (value.checked) {
                                    if (name) {
                                        if (value.name == name) {
                                            totalArr.push(value.value);
                                        }
                                    } else {
                                        totalArr.push(value.value);
                                    }
                                }
                            } else {
                                getArray(value.data, totalArr, name)
                            }
                        });
                        return totalArr;
                    }

                    var xtree = new layuiXtree({
                        elem: 'xtree',
                        form: form,
                        data: data,
                        isopen: false,  //加载完毕后的展开状态，默认值：true
                        ckall: true,    //启用全选功能，默认值：false
                        ckallback: function () {
                        }, //全选框状态改变后执行的回调函数
                        icon: {        //三种图标样式，更改几个都可以，用的是layui的图标
                            open: "&#xe619;",    //节点打开的图标
                            close: "&#xe61a;",   //节点关闭的图标
                            end: ""      //末尾节点的图标
                        },
                        color: {
                            open: "#EE9A00",       //节点图标打开的颜色
                            close: "#EE9A00",    //节点图标关闭的颜色
                            end: "#828282"       //末级节点图标的颜色
                        },
                        click: function (data) {
                            var oCks = xtree.GetChecked();

                            var district_Id = [];
                            var building_Id = [];
                            for (var i = 0; i < oCks.length; i++) {
                                var name = oCks[i].name;
                                params.name = name;

                                var id = oCks[i].value;
                                params.id = id;

                                if (params.name == "building") {
                                   /*console.log("请选择权限范围");*/

                                    var building_ids = id;
                                   /* console.log(building_ids);*/

                                    building_Id.push(building_ids);
                                    console.log(building_Id);

                                    params.building_Id = building_Id;

                                } else {
                                    var district_ids = params.id;
                                    district_Id.push(district_ids);
                                }
                            }//for
                            params.district_Id = district_Id;
                            console.log(params.district_Id);
                        }//click
                    })//xtree
                }//if
            }) //done
            .fail(function (response) {
                console.log(response)
                if (response.resultCode == '001') {
                    console.log("param is not found");
                } else if (response.resultCode == '009') {
                    console.log("String is not int type");
                }
            })

        $("#btn-s").off("click").on("click", function () {
/*            var wuyeID = localStorage.getItem("wuyeID");
            console.log(wuyeID);
            params.user_id = wuyeID;*/
            var set_Up = localStorage.getItem("set_Up");
            console.log(set_Up);
            params.user_id = set_Up;
/*          var paramsArr = duplicate(params.districtResult.concat(params.district_Id).concat(params.district_Ida));*/
/*          var paramsArr = duplicate(params.districtResult.concat(params.district_Id));*/
            var paramsArr = params.district_Id;
            console.log(paramsArr);
            var paramsdata = params.building_Id;
            console.log(paramsdata);

            $.ajax({
                url: '../../wuye_account/bindWuYeUserManage',
                traditional: true,
                type: 'post',
                dataType: "json",
                data: {
                    user_id: params.user_id,
                    district_id: paramsArr,
                    building_id: paramsdata
                },
            })
                .done(function (response) {
                    alert("设置成功");
                    window.parent.location.reload();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    if (response.resultCode == '200') {
                        console.log("success", response);
                    }
                })
                .fail(function (response) {
                    alert("设置失败");
                    window.parent.location.reload();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    if (response.resultCode == '001') {
                        console.log("param is not found");
                    }
                })
        })
        $("#btn-c").click(function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        })
    });

</script>
</body>
</html>
